<template>
	<div class="login" @keydown.enter="handleSubmit">
		<div class="login-con">
			<p class="title">
				欢迎登录
			</p>
			<div class="form-con">
				<Form ref="loginForm" :model="form" :rules="rules">
					<FormItem prop="userName">
						<Input v-model="form.userName" placeholder="请输入用户名">
						<span slot="prepend">
							<img src="../../../assets/img/user.png" alt="">
						</span>
						</Input>
					</FormItem>
					<FormItem prop="pwd">
						<Input type="password" v-model="form.pwd" placeholder="请输入密码">
						<span slot="prepend">
							<img src="../../../assets/img/lock.png" alt="">
						</span>
						</Input>
					</FormItem>
					<FormItem class="btn">
						<Button @click="handleSubmit" type="primary" long>登录</Button>
					</FormItem>
				</Form>
			</div>
		</div>
	</div>
</template>

<script>
	import Cookies from 'js-cookie';
	import API from "@/api/api";

	export default {
		data() {
			return {
				form: {
					userName: '',
					pwd: ''
				},
				rules: {
					userName: [{
						required: true,
						message: '账号不能为空',
						trigger: 'blur'
					}],
					pwd: [{
						required: true,
						message: '密码不能为空',
						trigger: 'blur'
					}]
				},
				redirectUrl: ''
			};
		},
		methods: {
			handleSubmit() {
				Cookies.remove('TOKEN');
				this.$refs.loginForm.validate((valid) => {
					if (valid) {
						API.login({
							username: this.form.userName,
							pwd: this.form.pwd
						}).then(res => {
							Cookies.set('TOKEN', res.data.token);
							Cookies.set('fullName', res.data.fullName);
							Cookies.set('loginType', res.data.loginType);
							Cookies.set('uploadServer', res.data.uploadServer);
							Cookies.set('uploadToken', res.data.uploadToken);
							Cookies.set('cdnUrl', res.data.cdnUrl);
							Cookies.set('p', res.data.permission);
							let redirectUrl = this.redirectUrl ? this.redirectUrl : '/index';
							this.$router.push(redirectUrl)
						}, error => {
							console.log(error)
						});
					}
				});
			}
		},
		created() {
			this.redirectUrl = this.$route.query.redirect;
		}
	};
</script>

<style lang="less" scoped>
	.login {
		width: 100%;
		height: 100%;
		background-image: url('../../../assets/img/loginBg.png');
		background-size: cover;
		background-position: center;
		position: relative;

		&-con {
			position: absolute;
			left: 20%;
			top: 50%;
			transform: translateY(-50%);
			width: 550px;
			height: 550px;

			.title {
				letter-spacing: 10px;
				font-size: 45px;
				line-height: 1;
				color: #4baafe;
			}

			// &-header {
			// 	font-size: 16px;
			// 	font-weight: 300;
			// 	text-align: center;
			// 	padding: 30px 0;
			// }

			.form-con {
				padding: 100px 0 0;
			}

			.login-tip {
				font-size: 10px;
				text-align: center;
				color: #c3c3c3;
			}
		}
	}
</style>
